package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增dish
     * @param dishDto
     * @return
     */

    @PostMapping
    public R save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(int page, int pageSize, String name) {
        Page dishDtoPage = dishService.findPage(page, pageSize, name);
        return R.success(dishDtoPage);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R updateFlavor(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }
    @GetMapping("/list")
    public R list(Dish dish){
        try {
            List<DishDto> list = dishService.list(dish);
            return R.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询菜品失败");
        }
    }


    /**
     * 删除，和批量删除
     */
    @DeleteMapping
    public R deleteDish(@RequestParam List<Long> ids){
        int rows = dishService.deleteDish(ids);
        if (rows>0){
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }

    }
    @PostMapping("/status/{status}")
    public R updateStatus(@RequestParam List<Long> ids,@PathVariable Integer status){

        int row = dishService.updateDishStatus(ids, status);
        if (row>0){
            return R.success("修改状态成功");
        }else{
            return R.error("修改状态失败");
        }

    }
}
